package offer;

/**
 * 描述:剑指 Offer 04. 二维数组中的查找
 *
 * @author Madison You
 * @created 0:09
 */
public class Mid_04_FindNumberIn2DArray {
    public static void main(String[] args) {
        int[][] arr = {{3}};
        new Mid_04_FindNumberIn2DArray().findNumberIn2DArray(arr, 3);
    }

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i].length == 0) return false;
            int left = 0;
            int right = matrix[i].length - 1;
            while (left < right) {
                int mid = (left + right) / 2;
                if (matrix[i][mid] > target) {
                    right = mid;
                } else if (matrix[i][mid] < target) {
                    left = mid + 1;
                } else {
                    return true;
                }
            }
            if (matrix[i][left] == target) {
                return true;
            }
        }
        return false;
    }
}
